package com.mbg17.utils;

import org.springframework.data.relational.core.sql.In;

import java.util.Arrays;
import java.util.HashMap;

public class LeetCode {
    public int numSpecial(int[][] mat) {
        int ans = 0;
        int[] rows = new int[mat.length];
        int[] cols = new int[mat[0].length];
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                if(mat[i][j]==1){
                    rows[i]+=1;
                    cols[j]+=1;
                }
            }
        }
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                if(rows[i]==1 && cols[j]==1 && mat[i][j]==1){
                    ans++;
                }
            }
        }
        return ans;
    }

    public int maximumSwap(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int max = num;
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars.length; j++) {
                char temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
                max = Math.max(max,Integer.parseInt(String.valueOf(chars)));
                chars[j] = chars[i];
                chars[i] = temp;
            }
        }
        return max;
    }

    public double trimMean(int[] arr) {
        Arrays.sort(arr);
        int s =( arr.length / 20);
        int e =( arr.length * 19 / 20);
        int sum = 0;
        int all = 0;
        for (int i = s; i < e; i++) {
            all +=arr[i];
            sum++;
        }
        return (double) all / sum;
    }

    public int maxLengthBetweenEqualCharacters(String s) {
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        char[] chars = s.toCharArray();
        int ans = 0;
        for (int i = 0; i < chars.length; i++) {
            Integer orDefault = characterIntegerHashMap.getOrDefault(chars[i], -1);
            if(orDefault==-1){
                characterIntegerHashMap.put(chars[i], i);
                orDefault = i;
            }
            ans = Math.max(i-orDefault-1,ans);
        }
        return ans;
    }
}
